<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="学号" prop="stunum">
      <el-input v-model="dataForm.stunum" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="stuname">
      <el-input v-model="dataForm.stuname" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="专业" prop="stusubject">
      <el-input v-model="dataForm.stusubject" placeholder=""></el-input>
    </el-form-item>
    <!-- <el-form-item label="" prop="isdel">
      <el-input v-model="dataForm.isdel" placeholder=""></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="图片路径" prop="stuPicture">
      <el-input v-model="dataForm.stuPicture" placeholder=""></el-input>
    </el-form-item> -->
    <el-form-item label="电话号码" prop="stuPhone">
      <el-input v-model="dataForm.stuPhone" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
 import { isMobile } from '@/utils/validate'
  export default {
    data () {
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          id: 0,
          stunum: '',
          stuname: '',
          stusubject: '',
          isdel: '',
          stuPicture: '',
          stuPhone: ''
        },
        dataRule: {
          stunum: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          stuname: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          stusubject: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          // isdel: [
          //   { required: true, message: '不能为空', trigger: 'blur' }
          // ],
          // stuPicture: [
          //   { required: true, message: '不能为空', trigger: 'blur' }
          // ],
          stuPhone: [
            { required: true, message: '不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/student/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.stunum = data.student.stunum
                this.dataForm.stuname = data.student.stuname
                this.dataForm.stusubject = data.student.stusubject
                this.dataForm.isdel = data.student.isdel
                this.dataForm.stuPicture = data.student.stuPicture
                this.dataForm.stuPhone = data.student.stuPhone
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/student/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'stunum': this.dataForm.stunum,
                'stuname': this.dataForm.stuname,
                'stusubject': this.dataForm.stusubject,
                'isdel': this.dataForm.isdel,
                'stuPicture': this.dataForm.stuPicture,
                'stuPhone': this.dataForm.stuPhone
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
